package com.tf.integrated.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ccssoft.m8.common.ccssoft.modules.util.EntityWrapperHandle;
import com.ccssoft.m8.common.web.BaseController;
import com.tf.integrated.entity.SearchMoreEntity;
import com.tf.integrated.entity.SearchMoreReturnEntity;
import com.tf.integrated.service.SearchMoreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 获取更多条件接口
 * @author allen
 * @date: 2020年9月14日上午10:56:20
 */
@Api(value = "获取更多条件接口",description="SearchMoreController")
@RestController
public class SearchMoreController extends BaseController
{
    @Autowired
    private SearchMoreService<SearchMoreEntity> searchMoreService;

    @ApiOperation("获取更多条件接口")
    @GetMapping("/search/more")
    @CrossOrigin(origins = "*",maxAge = 86400)
    public SearchMoreReturnEntity<JSONArray> searchDataList(@RequestParam Map<String, String> data) {
        JSONArray jsonArray = new JSONArray();
        if (data != null && data.size() > 0) {
            String family = data.get("family");
            if (isExist(family)) {
                String[] families = getStringArray(family);
                if (families.length > 0) {
                    EntityWrapperHandle<SearchMoreEntity> ew = new EntityWrapperHandle<SearchMoreEntity>();
                    ew.eq("resulttype", "family");
                    String sql = getSqlAndEqualOr("searchname", families);
                    ew.and(sql);
                    List<SearchMoreEntity> list = searchMoreService.selectList(ew);
                    if (list != null && list.size() > 0) {
                        for (SearchMoreEntity searchMoreEntity : list) {
                            JSONObject jsonObject = new JSONObject();
                            jsonObject.put("family", Integer.parseInt(searchMoreEntity.getSearchname()));
                            jsonObject.put("brand", JSONArray.parseArray(searchMoreEntity.getData()));
                            jsonArray.add(jsonObject);
                        }
                    }
                }
            }
        } else {
            EntityWrapperHandle<SearchMoreEntity> ew = new EntityWrapperHandle<SearchMoreEntity>();
            String[] types = new String[]{"publicStyle", "carStyle", "tricycleStyle", "personStyle"};
            String sql = getSqlAndEqualOr("resulttype", types);
            ew.where(sql);
            List<SearchMoreEntity> list = searchMoreService.selectList(ew);
            if (list != null && list.size() > 0) {
                for (SearchMoreEntity sme : list) {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("resultType", sme.getResulttype());
                    jsonObject.put("searchName", sme.getSearchname());
                    jsonObject.put("data", JSONArray.parseArray(sme.getData()));
                    jsonArray.add(jsonObject);
                }
            }
        }
        return new SearchMoreReturnEntity<JSONArray>().ok(jsonArray, 200);
    }

    @ApiOperation("获取更多条件接口")
    @PostMapping("/search/more")
    @CrossOrigin(origins = "*",maxAge = 86400)
    public SearchMoreReturnEntity<JSONArray> searchDataList(@RequestBody JSONArray body) {
        JSONArray jsonArray = new JSONArray();
        if (body != null && body.size() > 0) {
            String[] families = new String[body.size()];
            HashMap<String,List<String>> map = new HashMap<>();
            for(int i=0;i<families.length;i++){
                families[i]=body.getJSONObject(i).getString("family");
                map.put(families[i],body.getJSONObject(i).getJSONArray("brand").toJavaList(String.class));
            }
            EntityWrapperHandle<SearchMoreEntity> ew = new EntityWrapperHandle<SearchMoreEntity>();
            String sql = getSqlAndEqualOr("resulttype",families);
            ew.where(sql);
            List<SearchMoreEntity> list = searchMoreService.selectList(ew);
            for(SearchMoreEntity searchMoreEntity:list){
                List<String> brands=map.get(searchMoreEntity.getResulttype());
                if(brands.contains(searchMoreEntity.getSearchname())){
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("family",Integer.parseInt(searchMoreEntity.getResulttype()));
                    jsonObject.put("brand",Integer.parseInt(searchMoreEntity.getSearchname()));
                    jsonObject.put("stylecode",JSONArray.parseArray(searchMoreEntity.getData()));
                    jsonArray.add(jsonObject);
                }
            }
        }
        return new SearchMoreReturnEntity<JSONArray>().ok(jsonArray,200);
    }

    public boolean isExist(String value) {
        return value != null && !value.equals("");
    }

    public String[] getStringArray(String s){
        if(isExist(s)){
            return s.split(",");
        }
        return new String[0];
    }

    public String getSqlAndEqualOr(String field,String[] values){
        StringBuilder sql= new StringBuilder();
        sql.append("(");
        for(int i=0;i<values.length;i++){
            sql.append(field).append("='").append(values[i]).append("'");
            if(i<values.length-1){
                sql.append(" or ");
            }else{
                sql.append(")");
            }
        }
        return sql.toString();
    }
}